
// Include system file
#include "r_cg_can.h"

// Include project file
#include "CAN.h"
#include "fet.h"
#include "nvm.h"
#include "control.h"
#include "faults.h"
#include "Main.h"
#include "Switches.h"
#include "Joystick.h"
#include "Serial.h"
#include "Battery.h"
#include "BMS_CAN.h"

structBmsCan stBmsCan = {0};
//提供给快速接收的回调函数，用于拷贝接收数据和置位接收标志位
void vBMSCanFastRxCallBack(UINT16 u16_id_hi,UINT16 u16_id_lo,UINT8 u8_length,UINT8 *pData)//200us中断回调
{
	UINT8 i=0;
	if((u8_length == 8)&&(u16_id_lo==0xFFF4))
	{
		switch(u16_id_hi)
		{
			case 0x9801:
				for(i=0;i<8;i++)
				{
					stBmsCan.unRx0x1801FFF4.Bytes[i] = pData[i];
				}
				stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1801FFF4.fRxUpdate=1;
			break;
			case 0x9802:
				for(i=0;i<8;i++)
				{
					stBmsCan.unRx0x1802FFF4.Bytes[i] = pData[i];
				}
				stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1802FFF4.fRxUpdate=1;
			break;
			case 0x9803:
				for(i=0;i<8;i++)
				{
					stBmsCan.unRx0x1803FFF4.Bytes[i] = pData[i];
				}
				stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1803FFF4.fRxUpdate=1;
			break;
			case 0x9804:
				for(i=0;i<8;i++)
				{
					stBmsCan.unRx0x1804FFF4.Bytes[i] = pData[i];
				}
				stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1804FFF4.fRxUpdate=1;
			break;
			case 0x9805:
				for(i=0;i<8;i++)
				{
					stBmsCan.unRx0x1805FFF4.Bytes[i] = pData[i];
				}
				stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1805FFF4.fRxUpdate=1;
			break;
			case 0x9806:
				for(i=0;i<8;i++)
				{
					stBmsCan.unRx0x1806FFF4.Bytes[i] = pData[i];
				}
				stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1806FFF4.fRxUpdate=1;
			break;
			default:break;
		}
	}
}
static void vProcBmsCanLost(void)//10ms
{
	UINT8 i=0;
	for(i=0;i<6;i++)
	{
		if(stBmsCan.unRxCtrl.stBmsCanRxCtrlArr[i].fRxUpdate)
		{
			stBmsCan.unRxCtrl.stBmsCanRxCtrlArr[i].fRxLost=0;
			stBmsCan.unRxCtrl.stBmsCanRxCtrlArr[i].u16LostCnt=0;
		}
	}
	for(i=0;i<6;i++)
	{
		if(stBmsCan.unRxCtrl.stBmsCanRxCtrlArr[i].u16LostCnt<BMS_CAN_LOST_TIME)
		{
			stBmsCan.unRxCtrl.stBmsCanRxCtrlArr[i].u16LostCnt++;
		}
		else
		{
			stBmsCan.unRxCtrl.stBmsCanRxCtrlArr[i].fRxLost=1;
		}
	}
}
typedef struct
{
	UINT8 u8BmsState;
	UINT8 u8CmdHighVoltageOn;
}structBmsCanLogic;
structBmsCanLogic stBmsCanLogic = {0};

void vProcBmsCanRx(void)//10ms
{
	UINT8 i=0;
	vProcBmsCanLost();
	if(stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1801FFF4.fRxUpdate)
	{
		stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1801FFF4.fRxUpdate=0;
	}
	if(stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1802FFF4.fRxUpdate)
	{
		stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1802FFF4.fRxUpdate=0;
	}
	if(stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1803FFF4.fRxUpdate)
	{
		stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1803FFF4.fRxUpdate=0;
	}
	if(stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1804FFF4.fRxUpdate)
	{
		stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1804FFF4.fRxUpdate=0;
	}
	if(stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1805FFF4.fRxUpdate)
	{
		stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1805FFF4.fRxUpdate=0;
		stBmsCanLogic.u8BmsState = stBmsCan.unRx0x1805FFF4.bit.u8BatteryState;
	}
	if(stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1806FFF4.fRxUpdate)
	{
		stBmsCan.unRxCtrl.bit.stBmsCanRxCtrl0x1806FFF4.fRxUpdate=0;
	}
}
void vProcBmsCanTx(void)//10ms
{
	static UINT8 u8100msCnt=0;
	UINT8 i=0;
	UINT8 u8_buffer[8];
	if(++u8100msCnt>=10){u8100msCnt=0;}
	switch(u8100msCnt)
	{
		case 1:
			if(TX_FIFO_PUSH_EN)
			{
				
				for(i=0;i<8;i++)
				{
					u8_buffer[i] = stBmsCan.unTx0x1800F4FF.Bytes[i];
				}
				vPushCanTxFifo(&stCanTxFifo,0x9800, 0xF4FF, 8, u8_buffer);
			}
		break;
		case 2:
			if(TX_FIFO_PUSH_EN)
			{
				for(i=0;i<8;i++)
				{
					u8_buffer[i] = stBmsCan.unTx0x1801F4FF.Bytes[i];
				}
				vPushCanTxFifo(&stCanTxFifo,0x9801, 0xF4FF, 8, u8_buffer);
			}
		break;
		case 3:
			if(TX_FIFO_PUSH_EN)
			{
				for(i=0;i<8;i++)
				{
					u8_buffer[i] = stBmsCan.unTx0x18FD8C27.Bytes[i];
				}
				vPushCanTxFifo(&stCanTxFifo,0x98FD, 0x8C27, 8, u8_buffer);
			}
		break;
		default:
		break;
	}
}

